﻿using System;
using System.Collections.Generic;
using System.Text;
using com.dz.ht.common;
using System.Text.RegularExpressions;
using System.Web;


namespace com.dz.ht.components.book.site_1
{
    public class ChapterSiteInfo
    {
        public string SiteUrl { get; set; }

        public Encoding SiteEncoding { get; set; }

        public string ChapterContent { get; set; }

        public string RegTxt { get; set; }


    }

    public class ChapterCollectManager
    {
        /// <summary>
        /// 获取章节内容
        /// </summary>
        /// <param name="i_url"></param>
        /// <returns></returns>
        public string GetChapterContent(string i_url)
        {
            string t_chapter_content = string.Empty;

            List<ChapterSiteInfo> t_chapter_site_list=GetChapterSite(i_url);

            List< ChapterSiteInfo> t_chapter_site = t_chapter_site_list.FindAll(delegate(ChapterSiteInfo t_site)
            {
                return i_url.ToLower().IndexOf(t_site.SiteUrl) > -1;
            });

            if (t_chapter_site == null || t_chapter_site.Count<=0)
                return string.Empty;

            if (!string.IsNullOrEmpty(t_chapter_site[0].ChapterContent))
                return t_chapter_site[0].ChapterContent;

            try
            {
                string t_html = NetSiteCatchManager.ReadUrl(t_chapter_site[0].SiteUrl, t_chapter_site[0].SiteEncoding);

                if (!string.IsNullOrEmpty(t_html))
                {
                    Match t_mc = Regex.Match(t_html, t_chapter_site[0].RegTxt, RegexOptions.IgnoreCase | RegexOptions.Compiled);

                    if (t_mc != null)
                    {
                        t_chapter_content = t_mc.Groups[1].Value.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取章节内容失败" + ex.ToString());
            }

            return t_chapter_content;
        }


        private List<ChapterSiteInfo> GetChapterSite(string i_url)
        {
            List<ChapterSiteInfo> t_site_list = new List<ChapterSiteInfo>();

            ChapterSiteInfo t_site = new ChapterSiteInfo();
            t_site.SiteUrl = "tieba.baidu.com";
            t_site.ChapterContent = GetContentFromTieBa(i_url);
            t_site_list.Add(t_site);

            t_site = new ChapterSiteInfo();
            t_site.SiteUrl = "www.bj-ibook.cn";
            t_site.RegTxt = "<div[^<>]*?id=[\'\"]?bmsy_content[\'\"]?\\s*?name=[\'\"]?bmsy_content[\'\"]?\\s*?class=[\'\"]?cDiv[\'\"]?>(.*?)</div>";
            t_site.SiteEncoding = Encoding.GetEncoding("gb2312");

            t_site_list.Add(t_site);

            t_site = new ChapterSiteInfo();
            t_site.SiteUrl = "www.paoshu8.com";
            t_site.SiteEncoding = Encoding.GetEncoding("gb2312");
            t_site.RegTxt="<div\\s*?id=[\'\"]?BookText[\'\"]?>(.*?)</div>";
            t_site_list.Add(t_site);

            t_site = new ChapterSiteInfo();
            t_site.SiteUrl = "www.laishu.com";
            t_site.SiteEncoding = Encoding.GetEncoding("gb2312");
            t_site.RegTxt="<TD\\s*?id=[\'\"]?txt_td[\'\"]?\\s*?class=[\'\"]?read[\'\"]?\\s*?>(.*?)</td>";
            t_site_list.Add(t_site);

            t_site=new ChapterSiteInfo();
            t_site.SiteUrl="www.86zw.com";
            t_site.SiteEncoding=Encoding.GetEncoding("gb2312");
            t_site.RegTxt="<div\\s*?id=[\'\"]?BookText[\'\"]?\\s*?>(.*?)</div>";
            t_site_list.Add(t_site);

            t_site=new ChapterSiteInfo();
            t_site.SiteUrl="www.88106.com";
            t_site.SiteEncoding=Encoding.GetEncoding("gbk");
            t_site.RegTxt="<div\\s*?class=[\'\"]?Content[\'\"]?\\s*?id=[\'\"]?content[\'\"]?>(.*?)</div>";
            t_site_list.Add(t_site);

            t_site = new ChapterSiteInfo();
            t_site.SiteUrl = "www.00ks.com";
            t_site.SiteEncoding = Encoding.GetEncoding("gb2312");
            t_site.RegTxt = " <Div\\s*?id=[\'\"]?BookText[\'\"]?\\s*?>(.*?)</div>";

            t_site_list.Add(t_site);

            t_site = new ChapterSiteInfo();
            t_site.SiteUrl = "www.waok.net";
            t_site.SiteEncoding = Encoding.Default;
            t_site.RegTxt = "<div\\s*?style=[\'\"]?color:#000000;line-height:24px;[\'\"]?>(.*?)</div>";
            t_site_list.Add(t_site);

            t_site = new ChapterSiteInfo();
            t_site.SiteUrl = "www.docin.net";
            t_site.SiteEncoding = Encoding.GetEncoding("gbk");
            t_site.RegTxt = "<div\\s*?class=[\'\"]?main_n[\'\"]?\\s*?id=[\'\"]?content[\'\"]?>(.*?)</div>";

            t_site_list.Add(t_site);

            t_site = new ChapterSiteInfo();
            t_site.SiteUrl = "www.87ks.com";
            t_site.SiteEncoding = Encoding.GetEncoding("gb2312");
            t_site.RegTxt = "<DIV\\s*?id=[\'\"]?content[\'\"]?>(.*?)</div>";
            t_site_list.Add(t_site);

            t_site = new ChapterSiteInfo();
            t_site.SiteUrl = "www.aishu5.com";
            t_site.SiteEncoding = Encoding.GetEncoding("gbk");
            t_site.RegTxt = "<DIV\\s*?id=[\'\"]?content[\'\"]?>(.*?)</div>";
            t_site_list.Add(t_site);

            t_site = new ChapterSiteInfo();
            t_site.SiteUrl = "www.kanshu5.com";
            t_site.SiteEncoding = Encoding.GetEncoding("gbk");
            t_site.RegTxt = "<DIV\\s*?id=[\'\"]?content[\'\"]?>(.*?)</div>";
            t_site_list.Add(t_site);

            t_site = new ChapterSiteInfo();
            t_site.SiteUrl = "www.huobook.com";
            t_site.SiteEncoding = Encoding.GetEncoding("gb2312");
            t_site.RegTxt = "<div\\s*?class=[\'\"]?tit_f[\'\"]?\\s*?id=[\'\"]?BookText[\'\"]?>(.*?)</div>";
            t_site_list.Add(t_site);

            t_site = new ChapterSiteInfo();
            t_site.SiteUrl = "www.wxguan.com";
            t_site.SiteEncoding = Encoding.GetEncoding("gbk");
            t_site.RegTxt = "<div\\s*?class=[\'\"]?bookcontent clearfix[\'\"]?\\s*?id=[\'\"]?BookText[\'\"]?>(.*?)</div>";
            t_site_list.Add(t_site);

            t_site = new ChapterSiteInfo();
            t_site.SiteUrl = "www.ranwen.com";
            t_site.SiteEncoding = Encoding.GetEncoding("gbk");
            t_site.RegTxt = "<DIV\\s*?id=[\'\"]?content[\'\"]?\\s*?name=[\'\"]?content[\'\"]?>(.*?)</div>";
            t_site_list.Add(t_site);

            t_site = new ChapterSiteInfo();
            t_site.SiteUrl = "www.jshuba.com";
            t_site.SiteEncoding = Encoding.GetEncoding("gbk");
            t_site.RegTxt = "<div\\s*?class=[\'\"]?fontm[\'\"]?\\s*?id=[\'\"]?readtext[\'\"]?\\s*?[^<>]*?>(.*?)</div>";
            t_site_list.Add(t_site);

            t_site = new ChapterSiteInfo();
            t_site.SiteUrl = "www.yankuai.com";
            t_site.SiteEncoding = Encoding.GetEncoding("gbk");
            t_site.RegTxt = "<div\\s*?id=[\'\"]?content[\'\"]?>(.*?)</div>";
            t_site_list.Add(t_site);

            return t_site_list;
        }

        /// <summary>
        /// 抓取贴吧里的vip章节
        /// </summary>
        /// <param name="i_url"></param>
        /// <returns></returns>
        public string GetContentFromTieBa(string i_url)
        {
            string t_html = string.Empty;
            string t_reg = "<li\\s*?class=[\'\"]?name[\'\"]?><a.*?>(.*?)</a>\\s*?</li><.*?<td[^<>]*?classs*=s*[\'\"s]?content[\'\"]*?>[^<>]*<p[^<>]*?classs*=s*[\'\"s]?floor[\'\"]*?>\\d{1,}.*?</p>[^<>]*<cc>(.*?)</cc>";
            Encoding t_encoding = Encoding.GetEncoding("gbk");
            t_html = NetSiteCatchManager.ReadUrl(i_url, t_encoding);
            string t_chapter_content = string.Empty;
            MatchCollection t_ma = Regex.Matches(t_html, t_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
            int t_index = 0;
            bool t_is_chapter = true;
            StringBuilder t_sb = new StringBuilder();
            string t_nick_name = string.Empty;
            if (t_ma != null)
            {
                foreach (Match t_mc in t_ma)
                {
                    if (t_mc.Groups.Count > 0)
                    {
                        if (t_index == 0)
                        {
                            t_nick_name = t_mc.Groups[1].Value.ToString().Trim();
                        }
                        t_index++;
                        //最多取5楼
                        if (t_index > 5  || t_mc.Groups[1].Value.ToString().Trim().Equals(t_nick_name)==false)
                            break;
                      
                        t_chapter_content = t_mc.Groups[2].Value.ToString();
                        //一百个字的才算章节内容，否则算回复内容
                     
                        t_sb.Append(t_chapter_content);
                        
                      
                    }
                }
            }

            t_chapter_content = t_sb.ToString();
            return t_chapter_content;
        }
    }
}
